package business;


import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.ListIterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.CollectivityClassDAO;
import dao.TeacherDAO;
import orm.CollectivityClass;
import orm.Teacher;

import util.DBHelper;
import util.Utility;


public class CollectivityClassInfoServlet extends HttpServlet {
	
	/**
	 * TeacherInfoServlet
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public CollectivityClassInfoServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String strFunc = request.getParameter("func");
		int PageSize = Utility.PageSize;
		Connection con = null;
		Statement stmt = null;
		if (strFunc.equals("load")) {
			CollectivityClassDAO collectivityClassDAO = new CollectivityClassDAO();
			try {
				request.setAttribute("pageNo", 1);
				ArrayList<CollectivityClass> al = collectivityClassDAO.getAllCollectivityClasss();
				if (al.size() % PageSize == 0) {
					request.setAttribute("totalPage",al.size()/PageSize);
				} else {
					request.setAttribute("totalPage",(al.size()/PageSize) + 1);
				}
				
				al = new ArrayList<CollectivityClass>();
				ListIterator<CollectivityClass> it = collectivityClassDAO.getAllCollectivityClasss().listIterator();
				int ii = 0;
				while (it.hasNext()) {
					CollectivityClass t = it.next();
					CollectivityClass t1 = new CollectivityClass();
					t1.setClassID(t.getClassID());
					t1.setClassName(t.getClassName());
					t1.setClassSort(t.getClassSort()-1);
					t1.setTeacherName(t.getTeacherName());
					t1.setEstablishDate(t.getEstablishDate());

					al.add(t1);
					ii++;
					if(ii > (PageSize-1)) {
						break;
					}
					
				}
				
				request.setAttribute("collectivityClassList", al);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			request.getRequestDispatcher("../" + Utility.JSP_CollectivityClassInfo).forward(request, response);
		} else if (strFunc.equals("edit")) {
			String ClassID = request.getParameter("ClassID");
			CollectivityClassDAO collectivityClassDAO = new CollectivityClassDAO();
			
			try {
				CollectivityClass collectivityClass = collectivityClassDAO.getClassID(ClassID);
				request.setAttribute("collectivityClass", collectivityClass);
				request.getRequestDispatcher("../" + Utility.JSP_CollectivityClassEdit).forward(request, response);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} else if (strFunc.equals("delete")) {
			String ClassID = request.getParameter("ClassID");
			try {
				con = DBHelper.getConnection();
				con.setAutoCommit(false);
				stmt = con.createStatement();
				
				CollectivityClassDAO collectivityClassDAO = new CollectivityClassDAO();
				CollectivityClass collectivityClass = collectivityClassDAO.getClassID(ClassID);

				collectivityClassDAO.deleteCollectivityClass(stmt, ClassID);

				con.commit();
				stmt.close();
				
				ArrayList al = null;
				al = new ArrayList<CollectivityClass>();
				ListIterator<CollectivityClass> it = collectivityClassDAO.getAllCollectivityClasss().listIterator();
				int ii = 0;
				while (it.hasNext()) {
					CollectivityClass t = it.next();
					CollectivityClass t1 = new CollectivityClass();
					t1.setClassID(t.getClassID());
					t1.setClassName(t.getClassName());
					t1.setClassSort(t.getClassSort()-1);
					t1.setTeacherName(t.getTeacherName());
					t1.setEstablishDate(t.getEstablishDate());

					al.add(t1);
					ii++;
					if(ii > (PageSize-1)) {
						break;
					}
					
				}
				
				String tmp = request.getParameter("HDPageNo");
				int nPageNo = Integer.parseInt(tmp); 
				ArrayList<CollectivityClass> alCollectivityClass = collectivityClassDAO.getAllCollectivityClasss();
				int totalPage = 0;
				if (alCollectivityClass.size() % PageSize == 0) {
					totalPage = alCollectivityClass.size()/PageSize;
					request.setAttribute("totalPage",totalPage);
				} else {
					totalPage = alCollectivityClass.size()/PageSize + 1;
					request.setAttribute("totalPage",totalPage);
				}
				
				
				request.setAttribute("pageNo", 1);
				
				request.setAttribute("collectivityClassList", al);
				
				request.getRequestDispatcher("../" + Utility.JSP_CollectivityClassInfo).forward(request, response);
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
				try {
					con.rollback();
				} catch (SQLException ee) {
					ee.printStackTrace();
				}
			}
		} else if (strFunc.equals("paging")) {
			
			String strSub = request.getParameter("sub");
			String tmp = request.getParameter("HDPageNo");
			int nPageNo = Integer.parseInt(tmp); 
			
			CollectivityClassDAO collectivityClassDAO = new CollectivityClassDAO();
			try {
//				request.setAttribute("pageNo", nPageNo);
				ArrayList<CollectivityClass> al = collectivityClassDAO.getAllCollectivityClasss();
				int totalPage = 0;
				if (al.size() % PageSize == 0) {
					totalPage = al.size()/PageSize;
					request.setAttribute("totalPage",totalPage);
				} else {
					totalPage = (al.size()/PageSize) + 1;
					request.setAttribute("totalPage",totalPage);
				}
				
				if (strSub == null)
				{
					
				} else if (strSub.equals("start")) {
					nPageNo = 1;
				} else if (strSub.equals("front")) {
					nPageNo--;
				} else if (strSub.equals("next")) {
					nPageNo++;
				} else if (strSub.equals("last")) {
					nPageNo = totalPage ;
				} 
				request.setAttribute("pageNo", nPageNo);
				al = new ArrayList<CollectivityClass>();
				ListIterator<CollectivityClass> it = collectivityClassDAO.getAllCollectivityClasss().listIterator();
				int ii = 0;
				while (it.hasNext()) {
					ii++;
					CollectivityClass t = it.next();
					if (ii <= PageSize * (nPageNo - 1))
						continue;
					
					CollectivityClass t1 = new CollectivityClass();
					t1.setClassID(t.getClassID());
					t1.setClassName(t.getClassName());
					t1.setClassSort(t.getClassSort()-1);
					t1.setTeacherName(t.getTeacherName());
					t1.setEstablishDate(t.getEstablishDate());
					
					al.add(t1);
					
					if(ii > PageSize * nPageNo - 1 ) {
						break;
					}
				}
				
				request.setAttribute("collectivityClassList", al);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			request.getRequestDispatcher("../" + Utility.JSP_CollectivityClassInfo).forward(request, response);
		} else if (strFunc.equals("searchPage")) {
	
			String strSub = request.getParameter("sub");
			String tmp = request.getParameter("PageNo");
			int nPageNo = Integer.parseInt(tmp);

			CollectivityClassDAO collectivityClassDAO = new CollectivityClassDAO();
			try {
//				request.setAttribute("pageNo", nPageNo);
				ArrayList<CollectivityClass> al = collectivityClassDAO.getAllCollectivityClasss();
				int totalPage = 0;
				if (al.size() % PageSize == 0) {
					request.setAttribute("totalPage",al.size()/PageSize);
				} else {
					request.setAttribute("totalPage",(al.size()/PageSize) + 1);
				}
				
				request.setAttribute("pageNo", nPageNo);
				al = new ArrayList<CollectivityClass>();
				ListIterator<CollectivityClass> it = collectivityClassDAO.getAllCollectivityClasss().listIterator();
				int ii = 0;
				while (it.hasNext()) {
					ii++;
					CollectivityClass t = it.next();
					if (ii <= PageSize * (nPageNo - 1))
						continue;
					
					CollectivityClass t1 = new CollectivityClass();
					t1.setClassID(t.getClassID());
					t1.setClassName(t.getClassName());
					t1.setClassSort(t.getClassSort()-1);
					t1.setTeacherName(t.getTeacherName());
					t1.setEstablishDate(t.getEstablishDate());
					
					al.add(t1);
					
					if(ii > PageSize * nPageNo - 1 ) {
						break;
					}
				}
				
				request.setAttribute("collectivityClassList", al);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			request.getRequestDispatcher("../" + Utility.JSP_CollectivityClassInfo).forward(request, response);
		} else if (strFunc.equals("searchCollectivityClassInfo")) {
			CollectivityClassDAO collectivityClassDAO = new CollectivityClassDAO();
			try {
				request.setAttribute("pageNo", 1);
				ArrayList<CollectivityClass> al = collectivityClassDAO.getAllCollectivityClasss();
				if (al.size() % PageSize == 0) {
					request.setAttribute("totalPage",al.size()/PageSize);
				} else {
					request.setAttribute("totalPage",(al.size()/PageSize) + 1);
				}
				
				al = new ArrayList<CollectivityClass>();
				ListIterator<CollectivityClass> it = collectivityClassDAO.getAllCollectivityClasss().listIterator();
				int ii = 0;
				while (it.hasNext()) {
					CollectivityClass t = it.next();
					CollectivityClass t1 = new CollectivityClass();
					t1.setClassID(t.getClassID());
					t1.setClassName(t.getClassName());
					t1.setClassSort(t.getClassSort()-1);
					t1.setTeacherName(t.getTeacherName());
					t1.setEstablishDate(t.getEstablishDate());

					al.add(t1);
					ii++;
					if(ii > (PageSize-1)) {
						break;
					}
					
				}
				
				request.setAttribute("collectivityClassList", al);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			request.getRequestDispatcher("../" + Utility.JSP_SearchCollectivityClassInfo).forward(request, response);
			
		} else if (strFunc.equals("searchCollectivityClassToPaging")) {
			String strSub = request.getParameter("sub");
			String tmp = request.getParameter("HDPageNo");
			int nPageNo = Integer.parseInt(tmp); 
			
			CollectivityClassDAO collectivityClassDAO = new CollectivityClassDAO();
			try {
//				request.setAttribute("pageNo", nPageNo);
				ArrayList<CollectivityClass> al = collectivityClassDAO.getAllCollectivityClasss();
				int totalPage = 0;
				if (al.size() % PageSize == 0) {
					totalPage = al.size()/PageSize;
					request.setAttribute("totalPage",totalPage);
				} else {
					totalPage = (al.size()/PageSize) + 1;
					request.setAttribute("totalPage",totalPage);
				}
				
				if (strSub == null)
				{
					
				} else if (strSub.equals("start")) {
					nPageNo = 1;
				} else if (strSub.equals("front")) {
					nPageNo--;
				} else if (strSub.equals("next")) {
					nPageNo++;
				} else if (strSub.equals("last")) {
					nPageNo = totalPage ;
				} 
				request.setAttribute("pageNo", nPageNo);
				al = new ArrayList<CollectivityClass>();
				ListIterator<CollectivityClass> it = collectivityClassDAO.getAllCollectivityClasss().listIterator();
				int ii = 0;
				while (it.hasNext()) {
					ii++;
					CollectivityClass t = it.next();
					if (ii <= PageSize * (nPageNo - 1))
						continue;
					
					CollectivityClass t1 = new CollectivityClass();
					t1.setClassID(t.getClassID());
					t1.setClassName(t.getClassName());
					t1.setClassSort(t.getClassSort()-1);
					t1.setTeacherName(t.getTeacherName());
					t1.setEstablishDate(t.getEstablishDate());
					
					al.add(t1);
					
					if(ii > PageSize * nPageNo - 1 ) {
						break;
					}
				}
				
				request.setAttribute("collectivityClassList", al);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			request.getRequestDispatcher("../" + Utility.JSP_SearchCollectivityClassInfo).forward(request, response);
	
		} else if (strFunc.equals("searchCollectivityClassToSearchPage")) {
			String strSub = request.getParameter("sub");
			String tmp = request.getParameter("PageNo");
			int nPageNo = Integer.parseInt(tmp);

			CollectivityClassDAO collectivityClassDAO = new CollectivityClassDAO();
			try {
//				request.setAttribute("pageNo", nPageNo);
				ArrayList<CollectivityClass> al = collectivityClassDAO.getAllCollectivityClasss();
				int totalPage = 0;
				if (al.size() % PageSize == 0) {
					request.setAttribute("totalPage",al.size()/PageSize);
				} else {
					request.setAttribute("totalPage",(al.size()/PageSize) + 1);
				}
				
				request.setAttribute("pageNo", nPageNo);
				al = new ArrayList<CollectivityClass>();
				ListIterator<CollectivityClass> it = collectivityClassDAO.getAllCollectivityClasss().listIterator();
				int ii = 0;
				while (it.hasNext()) {
					ii++;
					CollectivityClass t = it.next();
					if (ii <= PageSize * (nPageNo - 1))
						continue;
					
					CollectivityClass t1 = new CollectivityClass();
					t1.setClassID(t.getClassID());
					t1.setClassName(t.getClassName());
					t1.setClassSort(t.getClassSort()-1);
					t1.setTeacherName(t.getTeacherName());
					t1.setEstablishDate(t.getEstablishDate());
					
					al.add(t1);
					
					if(ii > PageSize * nPageNo - 1 ) {
						break;
					}
				}
				
				request.setAttribute("collectivityClassList", al);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			request.getRequestDispatcher("../" + Utility.JSP_SearchCollectivityClassInfo).forward(request, response);
			
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
